home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / os2 / gnuwget.zip / wget-1.4.3 / src / utils.c < prev    next >
C/C++ Source or Header  |  1997-02-14  |  19KB  |  888 lines

  1. /* Various functions of utilitarian nature.
  2.    Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19. #ifdef HAVE_CONFIG_H
  20. #  include <config.h>
  21. #endif /* HAVE_CONFIG_H */
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #ifdef HAVE_STRING_H
  26. #  include <string.h>
  27. #else
  28. #  include <strings.h>
  29. #endif /* HAVE_STRING_H */
  30. #include <ctype.h>
  31.  
  32. #ifdef HAVE_PWD_H
  33. # include <pwd.h>
  34. #endif
  35.  
  36. #ifdef WINDOWS
  37. # include <direct.h>
  38. # define mkdir(a, b) _mkdir(a)  /* bletch! */
  39. #endif /* WINDOWS */
  40.  
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #ifdef HAVE_UNISTD_H
  44. #  include <unistd.h>
  45. #endif
  46. #include <assert.h>
  47. #include <limits.h>
  48. #ifdef HAVE_UTIME_H
  49. #  include <utime.h>
  50. #endif
  51. #ifdef HAVE_SYS_UTIME_H
  52. # include <sys/utime.h>
  53. #endif
  54. #include <errno.h>
  55.  
  56. #include "wget.h"
  57. #include "utils.h"
  58. #include "options.h"
  59. #include "mtch.h"
  60.  
  61. #ifndef errno
  62. extern int errno;
  63. #endif
  64.  
  65. extern struct options opt;
  66.  
  67.  
  68. /* nmalloc, nrealloc and nstrdup exit the program if there is not
  69.    enough memory. nstrdup also implements strdup on systems that
  70.    do not have it. */
  71. void *
  72. nmalloc(size_t size)
  73. {
  74.    void *res;
  75.  
  76.    res = malloc(size);
  77.    if (!res)
  78.       memfatal("malloc");
  79.    return res;
  80. }
  81.  
  82. void *
  83. nrealloc(void *obj, size_t size)
  84. {
  85.    void *res;
  86.  
  87.    /* Not all Unixes have the feature of realloc() that calling it
  88.       with a NULL-pointer is the same as malloc(), but it is easy to
  89.       simulate. */
  90.    if (obj)
  91.       res = realloc(obj, size);
  92.    else
  93.       res = malloc(size);
  94.    if (!res)
  95.       memfatal("realloc");
  96.    return res;
  97. }
  98.  
  99. char *
  100. nstrdup(const char *s)
  101. {
  102.    char *s1;
  103.  
  104. #ifndef HAVE_STRDUP
  105.    int l;
  106.  
  107.    l = strlen(s);
  108.    s1 = malloc(l + 1);
  109.    if (!s1)
  110.       memfatal("strdup");
  111.    memcpy(s1, s, l + 1);
  112.    return s1;
  113. #else
  114.    s1 = strdup(s);
  115.    if (!s1)
  116.       memfatal("strdup");
  117.    return s1;
  118. #endif
  119. }
  120.  
  121. /* Croak the fatal memory error and bail out with non-zero exit
  122.    status. */
  123. void
  124. memfatal(const char *s)
  125. {
  126.    fprintf(opt.lfile, "%s: Not enough memory.\n", s);
  127.    exit(1);
  128. }
  129.  
  130. /* Copy the string formed by two pointers (one on the beginning, other
  131.    on the char after the last char) to a new, malloc-ed
  132.    location. 0-terminate it. */
  133. char *
  134. strdupdelim(const char *beg, const char *end)
  135. {
  136.    char *res;
  137.  
  138.    res = (char *)nmalloc(end - beg + 1);
  139.    memcpy(res, beg, end - beg);
  140.    res[end - beg] = '\0';
  141.    return res;
  142. }
  143.  
  144. /* Returns an error message for the error ERRNUM.  Requires more work.  */
  145. const char *
  146. uerrmsg(uerr_t errnum)
  147. {
  148.    switch (errnum)
  149.    {
  150.       case URLUNKNOWN:
  151.      return "Unknown/unsupported protocol";
  152.      break;
  153.       case URLBADPORT:
  154.      return "Invalid port specification";
  155.      break;
  156.       case URLBADHOST:
  157.      return "Invalid host name";
  158.      break;
  159.       default:
  160.      assert(0);
  161.    }
  162. }
  163.     
  164.  
  165. /* Parse a string containing comma-separated elements, and return a
  166.    vector of char pointers with the elements.  Spaces following the
  167.    commas are ignored.  */
  168. char **
  169. sepstring(const char *s)
  170. {
  171.    char **res;
  172.    const char *p;
  173.    int i;
  174.  
  175.    if (!s || !*s)
  176.       return NULL;
  177.    res = NULL;
  178.    p = s;
  179.    i = 0;
  180.    while (*s)
  181.    {
  182.       if (*s == ',')
  183.       {
  184.      res = (char **)nrealloc(res, (i + 2) * sizeof(char *));
  185.      res[i] = strdupdelim(p, s);
  186.      res[++i] = NULL;
  187.      ++s;
  188.      /* Skip the blanks following the ','. */
  189.      while (isspace(*s))
  190.         ++s;
  191.      p = s;
  192.       }
  193.       else
  194.      ++s;
  195.    }
  196.    res = (char **)nrealloc(res, (i + 2) * sizeof(char *));
  197.    res[i] = strdupdelim(p, s);
  198.    res[++i] = NULL;
  199.    return res;
  200. }
  201.  
  202. /* Compare s1 and s2 frontally; s1 must be a subset of s2.  E.g. if s1
  203.    is `/something', s2 must begin with `/something' to make frontcmp
  204.    return 1.  Otherwise, frontcmp will return 0. */
  205. int
  206. frontcmp(const char *s1, const char *s2)
  207. {
  208.    for (; *s1 && *s2 && (*s1 == *s2); ++s1, ++s2);
  209.    return !*s1;
  210. }
  211.  
  212. /* A cuserid() immitation using getpwuid(), to avoid hassling with
  213.    utmp.  Besides, not all systems have cuerid(). */
  214.  
  215. char *
  216. mycuserid(char *where)
  217. {
  218.  
  219. #ifdef WINDOWS
  220.    if (where)
  221.       return nstrdup("");
  222.    else
  223.       return NULL;
  224. #else /* not WINDOWS */
  225.    struct passwd *pwd;
  226.  
  227.    if (!(pwd = getpwuid(getuid())) || !pwd->pw_name)
  228.       return NULL;
  229.    if (where)
  230.    {
  231.       strcpy(where, pwd->pw_name);
  232.       return where;
  233.    }
  234.    else
  235.       return pwd->pw_name;
  236. #endif /* not WINDOWS */
  237. }
  238.  
  239. /* Canonicalize PATH, and return a new path.  The new path differs from PATH
  240.    in that:
  241.     Multple `/'s are collapsed to a single `/'.
  242.     Leading `./'s and trailing `/.'s are removed.
  243.     Trailing `/'s are removed.
  244.     Non-leading `../'s and trailing `..'s are handled by removing
  245.     portions of the path.
  246.  
  247.    E.g. "a/b/c/./../d/.." will yield "a/b".
  248.  
  249.    Changes by hniksic:
  250.     Always use '/' as stub_char.
  251.     Don't check for local things using canon_stat.
  252.     Change the original string instead of strdup-ing.
  253.     React correctly when beginning with `./' and `../'.  */
  254. void
  255. path_simplify(char *path)
  256. {
  257.    register int i, start, ddot;
  258.    char stub_char;
  259.  
  260.    if (!*path)
  261.       return;
  262.  
  263.    /*stub_char = (*path == '/') ? '/' : '.';*/
  264.    stub_char = '/';
  265.  
  266.    /* Addition: Remove all `./'-s preceding the string.  If `../'-s
  267.       precede, put `/' in front and remove them too.  */
  268.    i = 0;
  269.    ddot = 0;
  270.    while (1)
  271.    {
  272.       if (path[i] == '.' && path[i + 1] == '/')
  273.      i += 2;
  274.       else if (path[i] == '.' && path[i + 1] == '.' && path[i + 2] == '/')
  275.       {
  276.      i += 3;
  277.      ddot = 1;
  278.       }
  279.       else
  280.      break;
  281.    }
  282.    if (i)
  283.       strcpy(path, path + i - ddot);
  284.  
  285.    /* Replace single `.' or `..' with `/'. */
  286.    if ((path[0] == '.' && path[1] == '\0')
  287.        || (path[0] == '.' && path[1] == '.' && path[2] == '\0'))
  288.    {
  289.       path[0] = stub_char;
  290.       path[1] = '\0';
  291.       return;
  292.    }
  293.    /* Walk along PATH looking for things to compact. */
  294.    i = 0;
  295.    while (1)
  296.    {
  297.       if (!path[i])
  298.      break;
  299.  
  300.       while (path[i] && path[i] != '/')
  301.      i++;
  302.  
  303.       start = i++;
  304.  
  305.       /* If we didn't find any slashes, then there is nothing left to do. */
  306.       if (!path[start])
  307.      break;
  308.  
  309.       /* Handle multiple `/'s in a row. */
  310.       while (path[i] == '/')
  311.      i++;
  312.  
  313.       if ((start + 1) != i)
  314.       {
  315.      strcpy (path + start + 1, path + i);
  316.      i = start + 1;
  317.       }
  318.  
  319.       /* Check for trailing `/'. */
  320.       if (start && !path[i])
  321.       {
  322.     zero_last:
  323.      path[--i] = '\0';
  324.      break;
  325.       }
  326.  
  327.       /* Check for `../', `./' or trailing `.' by itself. */
  328.       if (path[i] == '.')
  329.       {
  330.      /* Handle trailing `.' by itself. */
  331.      if (!path[i + 1])
  332.         goto zero_last;
  333.  
  334.      /* Handle `./'. */
  335.      if (path[i + 1] == '/')
  336.      {
  337.         strcpy (path + i, path + i + 1);
  338.         i = (start < 0) ? 0 : start;
  339.         continue;
  340.      }
  341.  
  342.      /* Handle `../' or trailing `..' by itself. */
  343.      if (path[i + 1] == '.' &&
  344.          (path[i + 2] == '/' || !path[i + 2]))
  345.      {
  346.         while (--start > -1 && path[start] != '/');
  347.         strcpy (path + start + 1, path + i + 2);
  348.         i = (start < 0) ? 0 : start;
  349.         continue;
  350.      }
  351.       }    /* path == '.' */
  352.    } /* while */
  353.  
  354.    if (!*path)
  355.    {
  356.       *path = stub_char;
  357.       path[1] = '\0';
  358.    }
  359. }
  360.  
  361. /* "Touch" FILE, i.e. make its atime and mtime equal to the time
  362.    specified with TM.  */
  363. void
  364. my_touch(char *file, time_t tm)
  365. {
  366. #ifdef HAVE_STRUCT_UTIMBUF
  367.    struct utimbuf times;
  368.    times.actime = times.modtime = tm;
  369. #else
  370.    time_t times[2];
  371.    times[0] = times[1] = tm;
  372. #endif
  373.  
  374.    if (utime(file, ×) == -1)
  375.    {
  376.       if (!opt.quiet)
  377.      fprintf(opt.lfile, "utime: %s\n", mystrerror(errno));
  378.    }
  379. }
  380.  
  381. /* Checks if a file is a symbolic link, and removes it if it is.  Does
  382.    nothing under MS-Windows.  */
  383. int
  384. remove_link(const char *file)
  385. {
  386.    int err = 0;
  387.    struct stat st;
  388.  
  389. #ifndef __EMX__
  390. #ifndef WINDOWS
  391.    if (lstat(file, &st) == 0 && S_ISLNK(st.st_mode))
  392.    {
  393. #ifdef DEBUG
  394.       if (opt.debug)
  395.      fprintf(opt.lfile, "Unlinking %s (symlink).\n", file);
  396. #endif
  397.       err = unlink(file);
  398.       if (err != 0)
  399.      if (opt.verbose)
  400.         fprintf(opt.lfile, "Failed to unlink symlink `%s': %s\n",
  401.             file, mystrerror(errno));
  402.    }
  403. #endif /* WINDOWS */
  404. #endif /* __EMX__ */
  405.    return err;
  406. }
  407.  
  408. /* Does a file exist? This is quite a lousy implementation, since it
  409.    supplies no error codes -- only a yes-or-no answer. Thus it will
  410.    return that a file does not exist if, e.g., the directory is
  411.    unreadable. I don't mind it too much currently, though.  The proper
  412.    way should, of course, be to have a third, error state, other than
  413.    true/false, but that would make the calling functions much more
  414.    complex. */
  415. int
  416. exists(const char *filename)
  417. {
  418.    struct stat buf;
  419.    return stat(filename, &buf) ? 0 : 1;
  420. }
  421.  
  422. /* Returns 0 if the path is a directory, 1 otherwise. Returns 0 on
  423.    error. */
  424. int
  425. isfile(const char *path)
  426. {
  427.    struct stat buf;
  428.    if (stat(path, &buf) != 0)
  429.       return 0;
  430.    return S_ISDIR(buf.st_mode) ? 0 : 1;
  431. }
  432.  
  433. /* The function that takes the dirname to be created, making sure that
  434.    missing directories are made one by one. Its behaviour should be
  435.    similar to mkdir -p on systems that support it. */
  436. int
  437. mymkdir(const char *d)
  438. {
  439.    int i, status, quit;
  440.    char *dir;
  441.    struct stat stbuf;
  442.  
  443.    /* Make a copy of dir, to be able to write to it. Otherwise, the
  444.       function is unsafe if called with a read-only char *argument. */
  445.    dir = nstrdup(d);
  446.    /* If the first character of dir is '/', skip it (and thus enable
  447.       creation of absolute-pathname directories. */
  448.    quit = 0;
  449.    for (i = (*dir == '/'); 1; ++i)
  450.    {
  451.       for (; dir[i] && dir[i] != '/'; i++)
  452.      ;
  453.       if (!dir[i])
  454.      quit = 1;
  455.       dir[i] = '\0';
  456.       /* Check whether the directory already exists.  */
  457.       status = stat(dir, &stbuf);
  458.       if (status != 0)
  459.       {
  460.      if (mkdir(dir, opt.dirmode) < 0)
  461.      {
  462.         free(dir);
  463.         return -1;
  464.      }
  465.       }
  466.       if (quit)
  467.      break;
  468.       else
  469.      dir[i] = '/';
  470.    } /* for */
  471.    free(dir);
  472.    return 0;
  473. }
  474.  
  475. /* Determine whether a file is acceptable to be followed, according to
  476.    lists of patterns to accept/reject. */
  477. int
  478. acceptable(const char *s)
  479. {
  480.    int l = strlen(s);
  481.  
  482.    while (l && s[l] != '/')
  483.       --l;
  484.    if (s[l] == '/')
  485.       s += (l + 1);
  486.    if (opt.accepts)
  487.    {
  488.       if (opt.rejects)
  489.      return (in_acclist((const char **)opt.accepts, s, 1)
  490.          && !in_acclist((const char **)opt.rejects, s, 1));
  491.       else
  492.      return in_acclist((const char **)opt.accepts, s, 1);
  493.    }
  494.    else if (opt.rejects)
  495.       return !in_acclist((const char **) opt.rejects, s, 1);
  496.    return 1;
  497. }
  498.  
  499. /* Returns whether a directory is acceptable for download, wrt
  500.    include/exclude lists.
  501.  
  502.    If the argument flags is ALLABS, the leading '/' is ignored in
  503.    paths; relative and absolute paths may be freely intermixed. */
  504. int
  505. accdir(const char *s, enum accd flags)
  506. {
  507.    char **x, *p;
  508.  
  509.    /* Remove starting '/'. */
  510.    if (flags & ALLABS && *s == '/')
  511.       ++s;
  512.    if (opt.includes)
  513.    {
  514.       for (x = opt.includes; *x; x++)
  515.       {
  516.      p = *x + (flags & ALLABS && **x == '/'); /* Remove '/' */
  517.      if (frontcmp(p, s))
  518.         break;
  519.       }
  520.       if (!*x)
  521.      return 0;
  522.    }
  523.    if (opt.excludes)
  524.    {
  525.       for (x = opt.excludes; *x; x++)
  526.       {
  527.      p = *x + (flags & ALLABS && **x == '/'); /* Remove '/' */
  528.      if (frontcmp(p, s))
  529.         break;
  530.       }
  531.       if (*x)
  532.      return 0;
  533.    }
  534.    return 1;
  535. }
  536.  
  537. /* Match a string against a pattern, backwards. E.g.:
  538.  
  539.    match_backwards("abc", "bc") -> 1
  540.    match_backwards("abc", "ab") -> 0
  541.    match_backwards("abc", "abc") -> 1 */
  542. int
  543. match_backwards(const char *string, const char *pattern)
  544. {
  545.    int i, j;
  546.  
  547.    for (i = strlen(string), j = strlen(pattern); i >= 0 && j >= 0; i--, j--)
  548.       if (string[i] != pattern[j])
  549.      break;
  550.    /* If the pattern was exhausted, the match was succesful. */
  551.    if (j == -1)
  552.       return 1;
  553.    else
  554.       return 0;
  555. }
  556.  
  557. /* Does a URL match each element of a list. List elements are matched
  558.    with fnmatch() or match_backwards(), according to whether the
  559.    pattern (or suffix) contains globbing characters.
  560.  
  561.    If the argument backward is unset, don't do backward comparison --
  562.    just compare them normally.  */
  563. int
  564. in_acclist(const char **accepts, const char *s, int backward)
  565. {
  566.    for (; *accepts; accepts++)
  567.    {
  568.       if (has_wildcards(*accepts))
  569.       {
  570.      /* fnmatch returns 0 if the pattern *does* match the
  571.         string. */
  572.      if (fnmatch(*accepts, s, 0) == 0)
  573.         return 1;
  574.       }
  575.       else
  576.       {
  577.      if (backward)
  578.      {
  579.         if (match_backwards(s, *accepts))
  580.            return 1;
  581.      }
  582.      else
  583.      {
  584.         if (!strcmp(s, *accepts))
  585.            return 1;
  586.      }
  587.       }
  588.    }
  589.    return 0;
  590. }
  591.  
  592. /* Return the malloc-ed suffix of a filename */
  593. char *
  594. suffix(const char *s)
  595. {
  596.    int i;
  597.  
  598.    for (i = strlen(s); i && s[i] != '/' && s[i] != '.'; i--);
  599.    if (s[i++] == '.')
  600.       return nstrdup(s + i);
  601.    else
  602.       return NULL;
  603. }
  604.  
  605. /* The function reads a whole line. It reads the line realloc-ing the
  606.    storage exponentially, doubling the storage after each overflow to
  607.    minimize the number of calls to realloc().
  608.  
  609.    It is not an exemplary of correctness, since it kills off the
  610.    newline (and no, there is no way to know if there was a newline at
  611.    EOF). */
  612. char *
  613. read_whole_line(FILE *fp)
  614. {
  615.    char *line;
  616.    int i, bufsize, c;
  617.  
  618.    i = 0;
  619.    bufsize = DYNAMIC_LINE_BUFFER;
  620.    line = nmalloc(bufsize);
  621.    /* Construct the line. */
  622.    while ((c = getc(fp)) != EOF && c != '\n')
  623.    {
  624.       if (i > bufsize - 1)
  625.      line = (char *)nrealloc(line, (bufsize <<= 1));
  626.       line[i++] = c;
  627.    }
  628.    if (c == EOF && !i)
  629.    {
  630.       free(line);
  631.       return NULL;
  632.    }
  633.    /* Check for overflow at zero-termination (no need to double the
  634.       buffer in this case. */
  635.    if (i == bufsize)
  636.       line = (char *)nrealloc(line, i + 1);
  637.    line[i] = '\0';
  638.    return line;
  639. }
  640.  
  641. /* Load file to memory, return the malloc-ed buffer, and the file
  642.    size. The file is loaded in chunks, each one double the size of the
  643.    previous one. The first chunk is FILE_BUFFER_SIZE bytes long. */
  644. void
  645. load_file(FILE *fp, char **buf, long *nread)
  646. {
  647.    long bufsize;
  648.  
  649.    bufsize = FILE_BUFFER_SIZE;
  650.    *nread = 0;
  651.    *buf = NULL;
  652.    while (!feof(fp))
  653.    {
  654.       *buf = (char *)nrealloc(*buf, bufsize + *nread);
  655.       *nread += fread(*buf + *nread, sizeof(char), bufsize, fp);
  656.       bufsize <<= 1;
  657.    }
  658. }
  659.  
  660. /* Free the pointers in a NULL-terminated vector of pointers, then
  661.    free the pointer itself. */
  662. void
  663. free_vec(char **vec)
  664. {
  665.    int i;
  666.  
  667.    if (!vec)
  668.       return;
  669.    for (i = 0; vec[i]; i++)
  670.       free(vec[i]);
  671.    free(vec);
  672. }
  673.  
  674. /* Merge the two vectors (v1 will be placed before of v2).  The
  675.    function effectively frees the vectors v1 and v2 (their contents
  676.    must not be reused after the call).  If v1 is NULL, the function
  677.    returns v2. */
  678. char **
  679. merge_vecs(char **v1, char **v2)
  680. {
  681.    int i, j;
  682.  
  683.    if (!v1)
  684.       return v2;
  685.    if (!v2)
  686.       return v1;
  687.    if (!*v2)
  688.    {
  689.       /* To avoid j == 0 */
  690.       free(v2);
  691.       return v1;
  692.    }
  693.    /* Count v1. */
  694.    for (i = 0; v1[i]; i++);
  695.    /* Count v2. */
  696.    for (j = 0; v2[j]; j++);
  697.    /* Reallocate v1. */
  698.    v1 = (char **)nrealloc(v1, (i + j + 1) * sizeof(char **));
  699.    memcpy(v1 + i, v2, (j + 1) * sizeof(char *));
  700.    free(v2);
  701.    return v1;
  702. }
  703.  
  704. /* A set of simple-minded routines to store and search for strings in
  705.    a linked list. You may add a string to the slist, and peek whether
  706.    it's still in there at any time later. */
  707.  
  708. /* Add an element to the list.  If flags is NOSORT, the list will not
  709.    be sorted.  */
  710. slist *
  711. add_slist(slist *l, const char *s, int flags)
  712. {
  713.    slist *t, *old, *beg;
  714.    int cmp;
  715.  
  716.    if (flags & NOSORT)
  717.    {
  718.       if (!l)
  719.       {
  720.      t = (slist *)nmalloc(sizeof(slist));
  721.      t->string = nstrdup(s);
  722.      t->next = NULL;
  723.      return t;
  724.       }
  725.       beg = l;
  726.       /* Find the last element. */
  727.       while (l->next)
  728.      l = l->next;
  729.       t = (slist *)nmalloc(sizeof(slist));
  730.       l->next = t;
  731.       t->string = nstrdup(s);
  732.       t->next = NULL;
  733.       return beg;
  734.    }
  735.    /* Empty list or changing the first element. */
  736.    if (!l || (cmp = strcmp(l->string, s)) > 0)
  737.    {
  738.       t = (slist *)nmalloc(sizeof(slist));
  739.       t->string = nstrdup(s);
  740.       t->next = l;
  741.       return t;
  742.    }
  743.  
  744.    beg = l;
  745.    if (cmp == 0)
  746.       return beg;
  747.  
  748.    /* Second two one-before-the-last element. */
  749.    while (l->next)
  750.    {
  751.       old = l;
  752.       l = l->next;
  753.       cmp = strcmp(s, l->string);
  754.       if (cmp == 0)             /* No repeating in the list. */
  755.      return beg;
  756.       else if (cmp > 0)
  757.      continue;
  758.       /* If the next list element is greater than s, put s between the
  759.      current and the next list element. */
  760.       t = (slist *)nmalloc(sizeof(slist));
  761.       old->next = t;
  762.       t->next = l;
  763.       t->string = nstrdup(s);
  764.       return beg;
  765.    }
  766.    t = (slist *)nmalloc(sizeof(slist));
  767.    t->string = nstrdup(s);
  768.    /* Insert the new element after the last element. */
  769.    l->next = t;
  770.    t->next = NULL;
  771.    return beg;
  772. }
  773.  
  774. /* Is there a specific entry in the list? */
  775. int
  776. in_slist(slist *l, const char *s)
  777. {
  778.    int cmp;
  779.  
  780.    while (l)
  781.    {
  782.       cmp = strcmp(l->string, s);
  783.       if (cmp == 0)
  784.      return 1;
  785.       else if (cmp > 0)         /* The list is ordered! */
  786.      return 0;
  787.       l = l->next;
  788.    }
  789.    return 0;
  790. }
  791.  
  792. /* Free the whole slist. */
  793. void
  794. free_slist(slist *l)
  795. {
  796.    slist *n;
  797.  
  798.    while (l)
  799.    {
  800.       n = l->next;
  801.       free(l->string);
  802.       free(l);
  803.       l = n;
  804.    }
  805. }
  806.  
  807. /* Legible -- return a static pointer to the legibly printed long. */
  808. char *
  809. legible(long l)
  810. {
  811.    static char buf[20];
  812.    char inbuf[20];
  813.    int i, i1, mod;
  814.    char *ptr, *in;
  815.  
  816.    /* Fill the buffer. */
  817.    prnum(inbuf, l);
  818.    /* Reset the pointers. */
  819.    ptr = buf;
  820.    in = inbuf;
  821.    /* If the number is negative, shift the pointers. */
  822.    if (*in == '-')
  823.    {
  824.       *ptr++ = '-';
  825.       ++in;
  826.    }
  827.    /* How many digits before the first separator? */
  828.    mod = strlen(in) % 3;
  829.    /* Insert them. */
  830.    for (i = 0; i < mod; i++)
  831.       *ptr++ = in[i];
  832.    /* Now insert the rest of them, putting separator before every
  833.       third digit. */
  834.    for (i1 = i, i = 0; in[i1]; i++, i1++)
  835.    {
  836.       if (i % 3 == 0 && i1 != 0)
  837.      *ptr++ = LEGIBLE_SEPARATOR;
  838.       *ptr++ = in[i1];
  839.    }
  840.    /* Zero-terminate the string. */
  841.    *ptr = '\0';
  842.    return buf;
  843. }
  844.  
  845. /* How many digits in a (long) integer? */
  846. int
  847. numdigit(long a)
  848. {
  849.    int res;
  850.  
  851.    for (res = 1; a /= 10; res++);
  852.    return res;
  853. }
  854.  
  855. /* Print a long integer to the string buffer.  The digits are first
  856.    written in reverse order (the least significant digit first), and
  857.    are then reversed.  */
  858. void
  859. prnum(char *where, long num)
  860. {
  861.    char *p;
  862.    int i = 0, l;
  863.    char c;
  864.  
  865.    if (num < 0)
  866.    {
  867.       *where++ = '-';
  868.       num = -num;
  869.    }
  870.    p = where;
  871.    /* Print the digits to the string. */
  872.    do
  873.    {
  874.       *p++ = num % 10 + '0';
  875.       num /= 10;
  876.    } while (num);
  877.    /* And reverse them. */
  878.    l = p - where - 1;
  879.    for (i = l/2; i >= 0; i--)
  880.    {
  881.       c = where[i];
  882.       where[i] = where[l - i];
  883.       where[l - i] = c;
  884.    }
  885.    where[l + 1] = '\0';
  886. }
  887.  
  888.